﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class frmNhaCungCap : Form
    {


        public frmNhaCungCap()
        {
            InitializeComponent();
        }
        int sttncc;
        NhaCungCapBUS NCC = new NhaCungCapBUS();
        private void frmNhaCungCap_Load(object sender, EventArgs e)
        {
            sttncc = 0;
            DataTable dt = NCC.DataNhaCungCap();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttncc++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewNhaCungCap.Rows.Add(sttncc, dr["MaNhaCungCap"].ToString(), dr["TenNhaCungCap"].ToString(), dr["DienThoai"].ToString(), email, dr["DiaChi"].ToString(), dr["Fax"].ToString(), dr["Website"].ToString(), dr["TaiKhoanNganHang"].ToString());

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            NhaCungCapDTO ncc = new NhaCungCapDTO();
            ncc.MaNhaCungCap = NCC.LayMaNhaCungCap() + 1;
            ncc.TenNhaCungCap = txtTenNhaCungCap.Text.Trim();
            ncc.SoDienThoai = txtSoDTNhaCungCap.Text.Trim();
            ncc.DiaChi = txtDiaCHiNhaCungCap.Text.Trim();
            ncc.Email = txtEMailNhaCungCap.Text.Trim();
            ncc.Fax = txtFax.Text.Trim();
            ncc.Website = txtWebSiteNhaCungCap.Text.Trim();
            ncc.TaiKhoanNganHang = txtTaiKhoanNganHang.Text.Trim();
            NCC.insert(ncc);
            dataGridViewNhaCungCap.Rows.Clear();
            sttncc = 0;
            DataTable dt = NCC.DataNhaCungCap();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttncc++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewNhaCungCap.Rows.Add(sttncc, dr["MaNhaCungCap"].ToString(), dr["TenNhaCungCap"].ToString(), dr["DienThoai"].ToString(), email, dr["DiaChi"].ToString(), dr["Fax"].ToString(), dr["Website"].ToString(), dr["TaiKhoanNganHang"].ToString());
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            NCC.Delete(int.Parse(txtMaNhaCungCap.Text));
            sttncc = 0;
            DataTable dt = NCC.DataNhaCungCap();
            dataGridViewNhaCungCap.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttncc++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewNhaCungCap.Rows.Add(sttncc, dr["MaNhaCungCap"].ToString(), dr["TenNhaCungCap"].ToString(), dr["DienThoai"].ToString(), email, dr["DiaChi"].ToString(), dr["Fax"].ToString(), dr["Website"].ToString(), dr["TaiKhoanNganHang"].ToString());

            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            NhaCungCapDTO ncc = new NhaCungCapDTO();
            ncc.MaNhaCungCap = int.Parse(txtMaNhaCungCap.Text.Trim());
            ncc.TenNhaCungCap = txtTenNhaCungCap.Text.Trim();
            ncc.SoDienThoai = txtSoDTNhaCungCap.Text.Trim();
            ncc.DiaChi = txtDiaCHiNhaCungCap.Text.Trim();
            ncc.Email = txtEMailNhaCungCap.Text.Trim();
            ncc.Fax = txtFax.Text.Trim();
            ncc.TaiKhoanNganHang = txtTaiKhoanNganHang.Text.Trim();
            NCC.update(ncc);
            dataGridViewNhaCungCap.Rows.Clear();
            sttncc = 0;
            DataTable dt = NCC.DataNhaCungCap();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttncc++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewNhaCungCap.Rows.Add(sttncc, dr["MaNhaCungCap"].ToString(), dr["TenNhaCungCap"].ToString(), dr["DienThoai"].ToString(), email, dr["DiaChi"].ToString(), dr["Fax"].ToString(), dr["Website"].ToString(), dr["TaiKhoanNganHang"].ToString());
            }
        }
        private void btnLàmMoi_Click(object sender, EventArgs e)
        {
            txtMaNhaCungCap.Clear();
            txtTenNhaCungCap.Clear();
            txtSoDTNhaCungCap.Clear();
            txtEMailNhaCungCap.Clear();
            txtDiaCHiNhaCungCap.Clear();
            txtFax.Clear();
            txtWebSiteNhaCungCap.Clear();
            txtTaiKhoanNganHang.Clear();
        }
        private void dataGridViewDSKH_SelectionChanged(object sender, EventArgs e)
        {
            int dong = dataGridViewNhaCungCap.SelectedCells.Count;
            if (dong == 0)
                return;
            int bc = dataGridViewNhaCungCap.SelectedCells[0].RowIndex;
            txtMaNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[1].Value.ToString();
            txtTenNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[2].Value.ToString();
            txtSoDTNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[3].Value.ToString();
            txtEMailNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[4].Value.ToString();
            txtDiaCHiNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[5].Value.ToString();
            txtFax.Text = dataGridViewNhaCungCap.Rows[bc].Cells[6].Value.ToString();
            txtWebSiteNhaCungCap.Text = dataGridViewNhaCungCap.Rows[bc].Cells[7].Value.ToString();
            txtTaiKhoanNganHang.Text = dataGridViewNhaCungCap.Rows[bc].Cells[8].Value.ToString();




        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dlr == DialogResult.Yes)
            //    this.Close();
            Close();
        }
    }
}
